qui(
    function (require, exports, module) {
        window.VODCom = window.VODCom || {};
        VODCom.H5Player = (function () {
            var eventManager = VODCom.EventManager();
            var taskOption = null;
            var currentRunningName = null;

            eventManager.bindEvent("do_append", appendSourceHandler);
            var flvManager = {};

            function buildPlayer(playerId, videoId) {
                var videoElement = document.getElementById(videoId);
                if (flvjs.isSupported()) {
                    var flvPlayer = flvjs.createPlayer({
                        "type": "flv",
                        "segments": [
                        ]
                    });
                    flvPlayer.attachMediaElement(videoElement);
                    flvManager[playerId] = {
                        player: flvPlayer,
                        bindEvent: false,
                        video: videoElement,
                        appendMode: false,
                        appendList: [],
                        loadIndex: -1
                    };
                }
            }

            function play(playerId, urlArrayList) {
                stopAppendMode(playerId);
                taskOption.playerId = playerId;
                taskOption.sourceArr = urlArrayList.splice(1);
                taskOption.stop = true;
                taskOption.timerId = setInterval(segmentLoadTrigger, 500);
                var buildConfig = {type: "flv", segments: []};
                $.each(urlArrayList, function (i, element) {
                    buildConfig.segments.push({duration: element.duration, url: element.url })
                })
                var player = flvManager[playerId].player;
                if (player != null) {
                    player.unload();
                    player._mediaDataSource = buildConfig;
                    player.attachMediaElement(flvManager[playerId].video);
                    player._msectl._mediaSource.addEventListener('sourceended', function (event) {
                        taskOption.stop = false;
                        player.currentTime = player.currentTime;
                        console.log("[play]", player.currentTime, taskOption.lastEndTime)
                        if ((player.currentTime == taskOption.lastEndTime) && (player.paused || player.ended)) {
                            flvManager[playerId].control.playpause();
                        } else if (taskOption.shortFlag == true) {
                            console.log("[sourceended]", player.duration, player.currentTime);
                            if (player.duration > 1 || taskOption.sourceArr.length == 0) {
                                taskOption.shortFlag = false;
                                flvManager[playerId].control.playpause();
                            }
                        }
                    });
                    player.load();
                    if (flvManager[playerId].control != null) {
                        if (urlArrayList[0].duration >= 1000) {
                            flvManager[playerId].control.playpause();
                        } else {
                            taskOption.shortFlag = true;
                        }
                    } else {
                        player.play();
                    }
                }
            }

            function singlePlay(playerId, urlArrayList,initStop){
                var buildConfig = {type: "flv", segments: []};
                $.each(urlArrayList, function (i, element) {
                    buildConfig.segments.push({duration: element.duration, url: element.url })
                })
                var player = flvManager[playerId].player;
                if (player != null) {
                    player.unload();
                    player._mediaDataSource = buildConfig;
                    player.attachMediaElement(flvManager[playerId].video);
                }
                player.load();
                if(!initStop){
                    player.play();
                }
            }

            function startAppendMode(playerId, options) {
                stopAppendMode(playerId);
                taskOption.playerId = playerId;
                taskOption.nextIndex = parseInt(options.index) + 1;
                taskOption.maxIndex = parseInt(options.index) + 99999;
                taskOption.stop = true;
                taskOption.timerId = setInterval(queryAppendTrigger, 500);
                var elementData = flvManager[playerId];
                elementData.appendMode = true;
                var buildConfig = {type: "flv", segments: [
                    {url: options.url, duration: options.duration, name: options.name }
                ]};
                $(flvManager[playerId].video).bind("timeupdate", notifyCurrentPlayingImage);
                var player = elementData.player;
                if (player != null) {
                    player.unload();
                    player._mediaDataSource = buildConfig;
                    player.attachMediaElement(flvManager[playerId].video);
                    player._msectl._mediaSource.addEventListener('sourceended', function (event) {
                        taskOption.stop = false;
                        player.currentTime = player.currentTime;
                        console.log("[play]", player.currentTime, taskOption.lastEndTime)
                        if ((player.currentTime == taskOption.lastEndTime) && (player.paused || player.ended)) {
                            flvManager[playerId].control.playpause();
                        } else if (taskOption.shortFlag == true) {
                            console.log("[sourceended]", player.duration, player.currentTime);
                            if (player.duration > 1) {
                                taskOption.shortFlag = false;
                                flvManager[playerId].control.playpause();
                            }
                        }
                    });
                    player.load();
                    if (options.duration >= 1000) {
                        flvManager[playerId].control.playpause();
                    } else {
                        taskOption.shortFlag = true;
                    }
                }
            }

            function segmentLoadTrigger() {
                if (taskOption.stop == true) {
                    return;
                }
                if (taskOption.sourceArr.length == 0) {
                    taskOption.stop = true;
                    return;
                }
                var element = flvManager[taskOption.playerId];
                if (element.video.duration > 60 + element.video.currentTime) {
                    console.log("[segmentLoad]delay load");
                    return;
                }
                var rest = taskOption.sourceArr.splice(taskOption.shortFlag ? 2 : 30);
                var loadArr = taskOption.sourceArr;
                taskOption.sourceArr = rest;
                taskOption.stop = true;
                segmentLoadHandler(flvManager[taskOption.playerId].player, loadArr)
            }

            function queryAppendTrigger() {
                if (taskOption.stop == true) {
                    return;
                }
                if (taskOption.nextIndex >= taskOption.maxIndex) {
                    return;
                }
                var element = flvManager[taskOption.playerId];
                if (element.video.duration > 60 + element.video.currentTime) {
                    console.log("[queryAppendTrigger]delay query");
                    return;
                }
                taskOption.stop = true;
                eventManager.dispatchEvent({type: "need_append_mode", data: {playerId: taskOption.playerId, index: taskOption.nextIndex, quickMode: taskOption.shortFlag }})
            }

            function appendSourceHandler(data) {
                var element = flvManager[ data.playerId];
                if (element != null) {
                    appendSource(element.player, data.resource.list, element)
                }
            }

            function segmentLoadHandler(flvPlayer, loadArr) {
                var hasLength = flvPlayer._mediaDataSource.segments.length;
                var baseTime = flvPlayer.currentTime * 1000;
                var start = hasLength
                $.each(loadArr, function (i, element) {
                    flvPlayer._mediaDataSource.segments.push({duration: element.duration, url: element.url, timestampBase: baseTime, cors: true, withCredentials: false});
                    baseTime += element.duration;
                    hasLength++;
                })
                taskOption.lastEndTime = flvPlayer.duration;
                flvPlayer._transmuxer._controller._loadSegment(start);
            }

            function appendSource(flvPlayer, resourceArr, manager) {
                if (resourceArr.length == 0) {
                    return;
                }
                var hasLength = flvPlayer._mediaDataSource.segments.length;
                var baseTime = flvPlayer.duration * 1000;
                var start = hasLength
                var segments = [];
                $.each(resourceArr, function (i, element) {
                    segments.push($.extend({}, { duration:element.length,name:element.name,url:element.url }, {timestampBase: baseTime, cors: true, withCredentials: false}));
                    baseTime += element.length;
                    hasLength++;
                })

                $.each(segments, function (i, element) {
                    flvPlayer._mediaDataSource.segments.push(element)
                })
                console.log(" start ")
                taskOption.nextIndex = parseInt(resourceArr[resourceArr.length - 1].name) + 1;
                taskOption.lastEndTime = flvPlayer.duration;

                flvPlayer._transmuxer._controller._loadSegment(start);
            }

            function stopAppendMode(playerId) {
                if (flvManager[playerId].appendMode == true) {
                    flvManager[playerId].appendMode = false;
                }
                if(currentRunningName != null){
                    currentRunningName = null;
                    eventManager.dispatchEvent({ type:"change_running_img", data: {name: null }})
                }
                $(flvManager[playerId].video).unbind("timeupdate", notifyCurrentPlayingImage);
                initTaskOption();
            }

            function notifyCurrentPlayingImage(event) {
                var player = flvManager["flash"].player;
                var currentTime = player.currentTime * 1000;
                var tempArr = [].concat(player._mediaDataSource.segments);
                var currentName = null;
                $.each(tempArr, function (i, element) {
                    if (element.duration + element.timestampBase > currentTime) {
                        currentName = element.name;
                        return false;
                    }
                })
                if (currentName == null) {
                    currentRunningName = null;
                    eventManager.dispatchEvent({ type:"change_running_img", data: {name: null }})
                } else if (currentRunningName != currentName) {
                    currentRunningName = currentName;
                    eventManager.dispatchEvent({ type:"change_running_img", data: {name: currentName }});
                }
            }

            function initTaskOption() {
                if (taskOption != null) {
                    if (taskOption.timerId > 0) {
                        clearInterval(taskOption.timerId);
                        taskOption.timerId = -1;
                    }
                    taskOption.playerId = null;
                }
                taskOption = {
                    timerId: -1,
                    playerId: null,
                    stop: true,
                    nextIndex: -1,
                    maxIndex: -1,
                    shortFlag: false
                };
            }

            function bindControl(playerId) {
                var control = new VODCom.H5PlayerControl({
                    player: flvManager[playerId].video,
                        controlDisable:["progressBar","timeBar"],
                    container: $(flvManager[playerId].video).parent()

                })
                control.bindEvent();
                control.initVol();
                flvManager[playerId].control = control;
            }

            function getPlayerData(playerId) {
                return flvManager[playerId];
            }

            return {
                buildPlayer: buildPlayer,
                bindControl: bindControl,
                play: play,
                eventManager: eventManager,
                startAppendMode: startAppendMode,
                getPlayerData: getPlayerData,
                singlePlay:singlePlay
            }
        })()
    }
);